import requests
import json
import pymongo

url = f"https://api.web.360kan.com/v1/rank?cat=1&callback=__jp0"
response = requests.get(url)
# print(response)
datas = response.text
# print(datas, type(datas))
r = datas.split("__jp0(")
s = r[1].split(")")
# print(s[0])
d = json.loads(s[0])
# print(d, type(d))

client = pymongo.MongoClient(host="localhost", port=27017)
client.drop_database("360")
db = client.get_database("360")
collection = db.get_collection("总榜")
for i in d["data"]:
    print(f"名字{i['title']}, 全集{i['upinfo']}, 实施热度{i['pv']}")
    add = collection.insert_many([{"排名": "要什么排名", "名字": f"{i['title']}", "实时热度": f"{i['pv']}", "全集": f"{i['upinfo']}"}])
client.close()

